package com.lsh.oms.core.records;

import com.lsh.base.lang.Maps;
import com.lsh.oms.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;

import static com.lsh.base.lang.bean.Beans.getLong;
import static com.lsh.base.lang.bean.Beans.getString;
import static com.lsh.oms.core.records.core.Return.Status.*;

public class SpotReturn extends ReturnRecord {

    private static final Logger LOGGER = LoggerFactory.getLogger(SpotReturn.class);

    @Override
    public void start() {
        run(Collections.emptyMap());
    }

    @Override
    public void run(Map<String, Object> request) {
        preDeliver(Collections.emptyMap());
    }

    @Override
    public void preDeliver(Map<String, Object> request) {
        deliver(request);
    }

    @Override
    public void deliver(Map<String, Object> request) {
        LOGGER.info("return: {} deliver with request: {}", id, request);
        String routeId = getString(ext, "shipping.route_id");
        Long deliverymanId = getLong(ext, "shipping.deliveryman_id");
        change(Maps.newHashMap("route_id", routeId, "deliveryman_id", deliverymanId), $ -> $.status = DELIVERED, PENDING, PRE_DELIVER);
        this.routeId = routeId;
        this.deliverymanId = deliverymanId;
    }

    @Override
    public void refund(Map<String, Object> request) {
        throw BusinessException.unsupportedOperation();
    }

    @Override
    public void cancel(Map<String, Object> request) {
        throw BusinessException.unsupportedOperation();
    }
}
